package com.dai.zhenfujin.view;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.dai.zhenfujin.R;
import com.dai.zhenfujin.adapter.ShowImagesAdapter;
import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;

import java.util.ArrayList;
import java.util.List;

import uk.co.senab.photoview.PhotoViewAttacher;


public class ShowImagesDialog extends Dialog implements View.OnClickListener {

    private View mView;
    private Context mContext;
    private ShowImagesViewPager mViewPager;
    private TextView mIndexText;
    private List<String> mImgUrls;
    private List<String> mTitles;
    private List<View> mViews;
    private ShowImagesAdapter mAdapter;

    private FrameLayout mClose;
    private FrameLayout mShareOrSave;
    private FrameLayout fl_conversation;
    private RelativeLayout mRlPhotoTop;
    private RelativeLayout mRlPhotoBottom;
    private LinearLayout ll_save_or_share;
    private TextView tv_share;
    private TextView tv_save;
    private TextView tv_cancel;
    private TextView tv_like_count;
    private TextView tv_write_count;
    private TextView tv_content;

    private LinearLayout ll_like;
    private LinearLayout ll_write;

    private ImageView iv_like;

    private boolean isLike;


    private int position;
    private String content;

    public ShowImagesDialog(@NonNull Context context, List<String> imgUrls,  int position) {
        super(context, R.style.AppTheme);
        this.mContext = context;
        this.mImgUrls = imgUrls;
        this.position = position;
        initView();
        initData();
    }

    @SuppressLint("ClickableViewAccessibility")
    private void initView() {
        mView = View.inflate(mContext, R.layout.dialog_images_brower, null);

        mViewPager = mView.findViewById(R.id.vp_images);
        mViewPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        break;
                    case MotionEvent.ACTION_MOVE:
                        break;
                    case MotionEvent.ACTION_UP:
                        break;

                }
                return false;
            }
        });

        mClose = mView.findViewById(R.id.fl_close);
        mIndexText = mView.findViewById(R.id.tv_image_index);
        mRlPhotoTop = mView.findViewById(R.id.rl_photo_top);



        mClose.setOnClickListener(this);
        mTitles = new ArrayList<>();
        mViews = new ArrayList<>();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(mView);
        Window window = getWindow();
        WindowManager.LayoutParams wl = window.getAttributes();
        wl.x = 0;
        wl.y = 0;
        wl.gravity = Gravity.CENTER;
        window.setAttributes(wl);
        mViewPager.setCurrentItem(position);
        mIndexText.setText(position + 1 + "/" + mImgUrls.size());
    }

    private void initData() {
        PhotoViewAttacher.OnPhotoTapListener listener = new PhotoViewAttacher.OnPhotoTapListener() {
            @Override
            public void onPhotoTap(View view, float x, float y) {
            }

            @Override
            public void onOutsidePhotoTap() {

            }
        };
        for (int i = 0; i < mImgUrls.size(); i++) {
            final SubsamplingScaleImageView photoView = new SubsamplingScaleImageView(mContext);
            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            photoView.setLayoutParams(layoutParams);
          //  photoView.setOnPhotoTapListener(listener);
            //显示超长图
            RequestOptions options = new RequestOptions().diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true);
           // photoView.setImage(ImageSource.uri(mImgUrls.get(i)));

            Glide.with(mContext).asBitmap().load(mImgUrls.get(i)).apply(options).into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(Bitmap resource,  Transition<? super Bitmap> transition) {
                     photoView.setImage(ImageSource.bitmap(resource));
                }
            });

            /*photoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
                @Override
                public void onPhotoTap(View view, float v, float v1) {
                    mRlPhotoTop.setVisibility(mRlPhotoTop.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
                  //  mRlPhotoBottom.setVisibility(mRlPhotoBottom.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
                }

                @Override
                public void onOutsidePhotoTap() {

                }
            });*/

            mViews.add(photoView);
            mTitles.add(i + "");
        }

        mAdapter = new ShowImagesAdapter(mViews, mTitles);
        mViewPager.setAdapter(mAdapter);
        mIndexText.setText(1 + "/" + mImgUrls.size());
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mIndexText.setText(position + 1 + "/" + mImgUrls.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fl_close:
                dismiss();



            default:
                break;
        }
    }


    public boolean isLike() {
        return isLike;
    }

}
